<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['lease:lease:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lease:lease:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lease:lease:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['lease:lease:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="leaseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房屋Id" align="center" prop="id" />
      <el-table-column label="小区Id" align="center" prop="plotId" />
      <el-table-column label="房屋简介" align="center" prop="houseName" />
      <el-table-column label="房屋面积(m^2)" align="center" prop="homeArea" />
      <el-table-column label="卧室数量" align="center" prop="bedroom" />
      <el-table-column label="客厅数量" align="center" prop="drawing" />
      <el-table-column label="卫生间数量" align="center" prop="restroom" />
      <el-table-column label="朝向Id" align="center" prop="orientationId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.orientation"
            :value="scope.row.orientationId"
          />
        </template>
      </el-table-column>
      <el-table-column label="期望售价" align="center" prop="homePrice" />
      <el-table-column label="平方价格" align="center" prop="squarePrice" />
      <el-table-column label="审核状态" align="center" prop="isExamine">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.is_examine"
            :value="scope.row.isExamine"
          />
        </template>
      </el-table-column>
      <el-table-column label="上架状态" align="center" prop="isPutaway">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.is_putaway"
            :value="scope.row.isPutaway"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁类型" align="center" prop="leaseType" />
      <el-table-column label="省" align="center" prop="city1" />
      <el-table-column label="市" align="center" prop="city2" />
      <el-table-column label="区" align="center" prop="city3" />
      <el-table-column
        label="地址经度"
        align="center"
        prop="addressLongitude"
      />
      <el-table-column label="地址纬度" align="center" prop="addressLatitude" />
      <el-table-column label="具体地址" align="center" prop="address" />
      <el-table-column label="房屋标签" align="center" prop="labelList">
        <template slot-scope="scope">
          <span v-for="i in scope.row.labelList" :key="i"> {{ i }}<br /> </span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isExamine == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="onUpdateStatus(scope.row.id, 1, '审核')"
            v-hasPermi="['plot:plot:edit']"
            >通过</el-button
          >
          <el-button
            v-if="scope.row.isExamine == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="onUpdateStatus(scope.row.id, 2, '审核')"
            v-hasPermi="['plot:plot:remove']"
            >驳回</el-button
          >
          <el-button
            v-if="scope.row.isPutaway == 0 && scope.row.isExamine != 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="onUpdateStatus(scope.row.id, 1, '上下架')"
            v-hasPermi="['plot:plot:edit']"
            >上架</el-button
          >
          <el-button
            v-if="scope.row.isPutaway == 1 && scope.row.isExamine != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="onUpdateStatus(scope.row.id, 0, '上下架')"
            v-hasPermi="['plot:plot:remove']"
            >下架</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租房审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房屋简介" prop="houseName">
          <el-input v-model="form.houseName" placeholder="请输入房屋简介" />
        </el-form-item>
        <el-form-item label="房屋面积" prop="homeArea">
          <el-input v-model="form.homeArea" placeholder="请输入房屋面积" />
        </el-form-item>
        <el-form-item label="卧室数量" prop="bedroom">
          <el-input v-model="form.bedroom" placeholder="请输入卧室数量" />
        </el-form-item>
        <el-form-item label="客厅数量" prop="drawing">
          <el-input v-model="form.drawing" placeholder="请输入客厅数量" />
        </el-form-item>
        <el-form-item label="卫生间数量" prop="restroom">
          <el-input v-model="form.restroom" placeholder="请输入卫生间数量" />
        </el-form-item>
        <el-form-item label="卫生间数量" prop="restroom">
          <el-select v-model="form.labelList" multiple placeholder="请选择">
            <el-option
              v-for="item in form.labelList"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省" prop="city1">
          <el-select
            v-model="form.city1"
            placeholder="请选择"
            @change="citys2(form.city1)"
          >
            <el-option
              v-for="item in city1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city2">
          <el-select
            v-model="form.city2"
            placeholder="请选择"
            @change="citys3(form.city2)"
          >
            <el-option
              v-for="item in city2"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区" prop="city3">
          <el-select v-model="form.city3" placeholder="请选择">
            <el-option
              v-for="item in city3"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="朝向Id" prop="orientationId">
          <el-select v-model="form.orientationId" placeholder="请选择朝向Id">
            <el-option
              v-for="dict in dict.type.orientation"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁类型" prop="orientationId">
          <el-select v-model="form.leaseType" placeholder="请选择租赁类型">
            <el-option label="合租" value="1"></el-option>
            <el-option label="整租" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="期望售价" prop="homePrice">
          <el-input v-model="form.homePrice" placeholder="请输入期望售价" />
        </el-form-item>
        <el-form-item label="平方价格" prop="squarePrice">
          <el-input v-model="form.squarePrice" placeholder="请输入平方价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLease,
  getLease,
  delLease,
  addLease,
  updateLease,
  updateStatus,
} from "@/api/lease/lease";
import { getCity } from "@/api/house/myhouse";

export default {
  name: "Lease",
  dicts: ["is_putaway", "is_examine", "orientation"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租房审核表格数据
      leaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plotId: null,
        leaseType: null,
      },
      // 表单参数
      form: { labelList: [] },
      // 表单校验
      rules: {},
      //状态参数
      statusDto: {},
      //省
      city1: [],
      city2: [],
      city3: [],
    };
  },
  created() {
    this.getList();
    this.citys1();
  },
  methods: {
    //获取省
    citys1() {
      getCity(0).then((res) => {
        this.city1 = res;
        // console.log(res);
      });
    },
    //获取市
    citys2(id) {
      this.city2 = [];
      getCity(id).then((res) => {
        this.city2 = res;
        // console.log("2222");

        // console.log(res);
      });
    },
    //获取区
    citys3(id) {
      this.city3 = [];
      getCity(id).then((res) => {
        this.city3 = res;
        // console.log("3333");

        // console.log(res);
      });
    },
    /** 改变状态*/
    onUpdateStatus(id, flag, type) {
      this.statusDto.id = id;
      this.statusDto.flag = flag;
      this.statusDto.mes = type;
      updateStatus(this.statusDto).then((R) => {
        if (R.msg == "操作成功") {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        } else {
          this.$modal.msgSuccess("修改失败");
        }
      });
    },

    /** 查询租房审核列表 */
    getList() {
      this.loading = true;
      listLease(this.queryParams).then((response) => {
        this.leaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plotId: null,
        houseName: null,
        homeArea: null,
        bedroom: null,
        drawing: null,
        restroom: null,
        orientationId: null,
        homePrice: null,
        squarePrice: null,
        isExamine: null,
        isPutaway: null,
        createTime: null,
        leaseType: null,
        city1: null,
        city2: null,
        city3: null,
        addressLongitude: null,
        addressLatitude: null,
        address: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租房审核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLease(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租房审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLease(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLease(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除租房审核编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLease(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "lease/lease/export",
        {
          ...this.queryParams,
        },
        `lease_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
